package com.ypj.rpc.provider;

import com.ypj.rpc.enumeration.RpcError;
import com.ypj.rpc.exception.RpcException;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认的服务注册表，保存服务端本地服务
 */
@Slf4j
public class ServiceProviderImpl implements ServiceProvider{

    private static final Map<String,Object> serviceMap=new ConcurrentHashMap<>();
    private static final Set<String> registeredService=ConcurrentHashMap.newKeySet();

    @Override
    public <T> void addServiceProvider(T service) {
        String serviceName=service.getClass().getCanonicalName();
        if (registeredService.contains(serviceName)){
            return ;
        }
        registeredService.add(serviceName);
        Class<?>[] interfaces=service.getClass().getInterfaces();
        if (interfaces.length == 0) {
            throw new RpcException(RpcError.SERVICE_NOT_IMPLEMENT_ANY_INTERFACE.getMessage());
        }
        for (Class<?> i:interfaces){
            serviceMap.put(i.getCanonicalName(),service);
            log.info("为接口：{}注册服务：{}",interfaces,serviceName);
        }
    }

    @Override
    public Object getServiceProvider(String serviceName) {
        Object service=serviceMap.get(serviceName);
        if (service==null){
            log.error("未找到对应服务");
            throw new RpcException(RpcError.SERVICE_NOT_FOUND.getMessage());
        }
        return service;
    }
}
